package com.huyandroid.AccountMonitor1;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class ExchangeHandler extends SQLiteOpenHelper{
	///ALL STATIC VARIABLES for 1 DATABASE --CHANGED DEPEND EACH APPLICATION
			//Database:
			private static final int DATABASE_VERSION = 1;
			private static final String DATABASE_NAME = "exchangesManager"; ;
			String _DBname;
			//Table + Column names
			private static final String TABLE_DATA = "exchanges";
			private static final String KEY_ID = "id";   //Column 0
			private static final String KEY_COL1 = "exchange"; //Column 1
			private static final String KEY_COL2 = "balance"; //Column 2
			
			private static final String KEY_COL3= "time";  //Column 3
			private static final String KEY_COL4="place"; //Column 4
			
			private static final String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_DATA + "("
					+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_COL1 + " TEXT,"
					+ KEY_COL2 + " TEXT,"+ KEY_COL3 + " TEXT,"+ KEY_COL4 + " TEXT"+");";
			
			///Database Variables
			private Context mContext;
			private SQLiteDatabase mDB;
			////Constructors//////////////////////////////////////////////
			public ExchangeHandler(Context c)
			{
				
				super(c, DATABASE_NAME, null, DATABASE_VERSION);
				mContext =c;
				
			}
			public ExchangeHandler(Context c,String DBname)
			{
				super(c, DBname, null, DATABASE_VERSION);
				mContext =c;
				_DBname=DBname;
			}
			
			////////CREATE + UPDATE TABLE////////////////////////////////////
			@Override
			public void onCreate(SQLiteDatabase db) {
				// TODO Auto-generated method stub
				db.execSQL(CREATE_USERS_TABLE);
			}
			@Override
			public void onUpgrade(SQLiteDatabase  db, int oldVersion, int newVersion) {
				// TODO Auto-generated method stub
				db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATA);

				// Create tables again
				onCreate(db);
			}
			//////////////////OTHER STANDARD OPERATIONS////////////////////////
			public ExchangeHandler open(){
				//super(mContext, DATABASE_NAME, null, DATABASE_VERSION);
				mDB=this.getWritableDatabase();
				return this;
			}
			public void close()
			{
				mDB.close();
			}
			public long addItem(Exchange ec){
				ContentValues v = new ContentValues();
				
				v.put(KEY_COL1, ec.getExchange());
				v.put(KEY_COL2, ec.getBalance());
				
				v.put(KEY_COL3,ec.getTime());
				v.put(KEY_COL4,ec.getPlace());
				
				return mDB.insert(TABLE_DATA, null, v);
			}

			public List<Exchange> getAllItems(){
				List<Exchange> L = new ArrayList<Exchange>();
				String selectQuery = "SELECT  * FROM " + TABLE_DATA;
				Cursor c = mDB.rawQuery(selectQuery, null);
				if(c.moveToFirst()){
					do{
						Exchange ec=new Exchange();
						ec.setExchange(c.getString(1)); //Id of the table != _ID in the object User
						ec.setBalance(c.getString(2));//user balance
						ec.setTime(c.getString(3));//time
						ec.setPlace(c.getString(4));//place
						L.add(ec);
					}while (c.moveToNext());
				}
				return L;
			}
			public String[] getItem(long rowId){
				Cursor c=mDB.query(true, TABLE_DATA, new String[]{KEY_ID,KEY_COL1,KEY_COL2,KEY_COL3,KEY_COL4 }, 
						KEY_ID+"="+rowId,null, null, null, null, null);
				if(c!=null) c.moveToFirst();
				String[] temp=new String[5];
				for(int i=0;i<5;i++) temp[i]=c.getString(i);
				return temp;
				}
			public boolean updateItem(long rowId,String exchange,String balance,String time,String place){
				ContentValues v = new ContentValues();
				
				v.put(KEY_COL1,exchange);
				v.put(KEY_COL2,balance);
				v.put(KEY_COL3,time);
				v.put(KEY_COL4,place);
				return mDB.update(TABLE_DATA, v, KEY_ID+"="+rowId,null)>0;
			}
			public boolean databaseExist(){
				//method 1:
				
				String destPath="/data/data/com.huyandroid.AccountMonitor1/databases/"+_DBname;
				File f=new File(destPath);
				if(f.exists())return true;
				return false;
				//method2
			}
}
